//优先级队列的类
function PriorityQueue() {
    //空数组
    this.items = [];
    //数据项的内部类
    function QueueElement(element,priority) { 
        this.element = element;
        this.priority = priority;
     };
     //优先级队列的方法 enqueue dequeue front size isEmpty toString
     PriorityQueue.prototype.enqueue = function (element,priority) { 
         if(arguments.length<2) {
             throw new Error('请传入两个参数！（数据，优先级）')
         };
         if(typeof arguments[1] != 'number'){
            throw new TypeError('优先级参数必须是数字！')
         }
         //实例化一个项实例
         let queueElement = new QueueElement(element,priority);
         if(this.items.length==0) {//如果队列中没有元素
             this.items.push(queueElement);
             console.log('优先级队列中为空！',queueElement.element);
         }else{//如果队列中有元素，就先判断优先级，根据优先级判断插入的位置
            //标记是否被插入的变量
            let added =false;
            for(var i = 0;i<this.items.length;i++){
                if(queueElement.priority>this.items[i].priority ){
                    this.items.splice(i,0,queueElement);
                    added = true;
                    break;
                };
            };
            //循环结束仍然没有找到位置意味着他的优先级最小，直接插入到对尾即可
            if(!added){
                this.items.push(queueElement);
            }
            
         };
         return true
      };
    PriorityQueue.prototype.dequeue = function () { 
            return this.items.shift();
       };
    PriorityQueue.prototype.front = function () { 
        return this.items[0]
     };
    PriorityQueue.prototype.size = function () { 
        return this.items.length
     };
    PriorityQueue.prototype.isEmpty = function () { 
        return this.items.length == 0
     };
    PriorityQueue.prototype.toString = function () { 
        let resStr = '';
        this.items.forEach(item=>{
            resStr += item.element + ' '
        });
        return resStr
     }

  }
